home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 054 (1988-05-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 054 (1988-05-15)(Ossowski, Stefan)(DE)(PD).adf / MRBackup / MRBackup2.0 / IntuiHandler.c < prev    next >
C/C++ Source or Header  |  1988-04-09  |  10KB  |  429 lines

  1. /* Intuition handler for MRBackup
  2.  * Filename:    IntuiHandler.c
  3.  * Date:        11/21/87
  4.  *
  5.  * History:        (most recent change first)
  6.  *
  7.  * 11/21/87 -MRR- This file was created for version 2.0.
  8.  */
  9.  
  10. #include "MRBackup.h"
  11. #include "Screen.c"
  12.  
  13. extern struct GfxBase *GfxBase;
  14.  
  15. extern struct Gadget curVolumeGadget;
  16. extern struct Gadget errorGadget;
  17.  
  18. struct Window *OpenPathWindow();
  19.  
  20. /* Close all Intuition items. */
  21.  
  22. CloseIntuition()
  23. {
  24.     if ( IntuitionBase ) {
  25.         if ( mainScreen ) {
  26.             if ( mainWindow ) {
  27.                 ClearMenuStrip( mainWindow );
  28.                 CloseWindow( mainWindow );
  29.             }
  30.             if ( pathWindow )    CloseWindow( pathWindow );
  31.             CloseScreen( mainScreen );
  32.         }
  33.         if ( progressConsole ) DeleteConsole( progressConsole );
  34.         if ( progressWindow ) CloseWindow( progressWindow );
  35.  
  36. #ifdef DEBUG
  37.         if ( debugConsole ) DeleteConsole( debugConsole );
  38.         if ( debugWindow ) CloseWindow( debugWindow );
  39. #endif
  40.  
  41.         if ( GfxBase ) CloseLibrary( GfxBase );
  42.         CloseLibrary( IntuitionBase );
  43.     }                        /* end if IntuitionBase */
  44. }
  45. ^L
  46. /* Handle a gadget action.
  47.  * Called with:
  48.  *      window:        window that gadget is displayed in
  49.  *        class:        message class (GADGETUP/GADGETDOWN/?)
  50.  *        addr:        pointer to gadget structure
  51.  */
  52. DoGadget(window, class, addr)
  53.     struct Window *window; ULONG class; struct Gadget *addr;
  54. {
  55.     USHORT id;                    /* gadget identifier */
  56.     struct Gadget *upGad;
  57.     ULONG upClass;
  58.     struct IntuiMessage *upMsg;    /* require gadget up to complete */
  59.  
  60.     id = addr->GadgetID;
  61.  
  62.     if (class == GADGETUP) {
  63. #ifdef DEBUG
  64.         sprintf(debugMsg,"GADGETUP: %d\n",id);
  65.         DebugWrite(debugMsg);
  66. #endif
  67.         switch (id) {
  68.             case HOMEPATH:
  69.                 GetHomePath(addr);
  70.                 break;
  71.             case BACKPATH:
  72.                 GetBackPath(addr);
  73.                 break;
  74.             case LISTPATH:
  75.                 GetListPath(addr);
  76.                 break;
  77.             case XCLDPATH:
  78.                 GetXcldPath(addr);
  79.                 break;
  80.             case STOP:
  81.                 TypeAndSpeak(
  82.     "Use the STOP gadget during backup and restore operations.\n");
  83.                 break;
  84.             default:
  85. #ifdef DEBUG
  86.                 sprintf(conmsg, "Unknown gadget, ID %d.  Program error!\n",
  87.                         id);
  88.                 TypeAndSpeak(conmsg);
  89. #endif
  90.                 break;
  91.         }
  92.     }
  93. }
  94. int
  95. InitIntuition()
  96. {
  97.     struct ViewPort vP;
  98.     struct RastPort *rpG;
  99.  
  100.     IntuitionBase = (struct IntuitionBase *)
  101.         OpenLibrary("intuition.library", 0L);
  102.     if ( ! IntuitionBase ) {
  103.         puts("I can't open Intuition!\n");
  104.         return ERR_ABORT;
  105.     }
  106.  
  107.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L);
  108.     if ( ! GfxBase ) {
  109.         puts("I can't open the graphics library!\n");
  110.         return ERR_ABORT;
  111.     }
  112.  
  113.     mainScreen = OpenScreen(&NewScreenStructure);
  114.     if ( ! mainScreen ) {
  115.         puts("I can't open the screen!\n");
  116.         return ERR_ABORT;
  117.     }
  118.  
  119.     vP = mainScreen->ViewPort;
  120.     LoadRGB4(&vP, &Palette, (long) PaletteColorCount);
  121.  
  122.     NewWindowStructure1.Screen = mainScreen;
  123.     mainWindow = OpenWindow(&NewWindowStructure1);    /* open the window */
  124.     if ( mainWindow == NULL )
  125.     {
  126.         puts("I can't open the main window!\n");
  127.         return ERR_ABORT;
  128.     }
  129.  
  130.     if (!OpenPathWindow()) return ERR_ABORT;
  131.  
  132.  
  133.  
  134.     rpG = mainWindow->RPort;    /* get a rastport pointer for the window */
  135.  
  136.     SetMenuStrip(mainWindow, &MenuList1);
  137.  
  138.     /* Open the console(s) and related windows. */
  139.  
  140.     NewWindowStructure4.Screen = mainScreen;
  141.     if ( ! (progressWindow = OpenWindow( &NewWindowStructure4 ) ) ) {
  142.         puts("I can't open the progress window!\n");
  143.         return ERR_ABORT;
  144.     }
  145.  
  146.     if ( ! (progressConsole = CreateConsole( progressWindow ) ) ) {
  147.         puts("I can't create the progress window console!\n");
  148.         return ERR_ABORT;
  149.     }
  150.  
  151. #ifdef DEBUG
  152.     NewWindowStructure5.Screen = mainScreen;
  153.     if ( ! (debugWindow = OpenWindow( &NewWindowStructure5 ) ) ) {
  154.         puts("I can't open the debug window!\n");
  155.         return ERR_ABORT;
  156.     }
  157.  
  158.     if ( ! (debugConsole = CreateConsole( debugWindow ) ) ) {
  159.         puts("I can't create the debug window console!\n");
  160.         return ERR_ABORT;
  161.     }
  162. #endif
  163.  
  164. #ifdef IntuiTextList1
  165.     PrintIText(rpG,&IntuiTextList1,0L,0L);    /* Print the text if any. */
  166. #endif
  167.  
  168. #ifdef BorderList1
  169.     DrawBorder(rpG,&BorderList1,0L,0L);    /* Draw the borders if any */
  170. #endif
  171.  
  172. #ifdef ImageList1
  173.     DrawImage(rpG,&ImageList1,0L,0L);    /* Draw the images if any. */
  174. #endif
  175.     return ERR_NONE;
  176. }
  177.  
  178. /* Open the pathname specification window.
  179.  * Returns:
  180.  *        pointer to new window (success) or NULL (failure)
  181.  * Side-Effects:
  182.  *        sets pathWindow to new window pointer
  183.  */
  184.  
  185. struct Window *
  186. OpenPathWindow()
  187. {
  188.     if (!pathWindow) {                /* only if not already open */
  189.         NewWindowStructure3.Screen = mainScreen;
  190.         if (! ( pathWindow = OpenWindow(&NewWindowStructure3)) ) {
  191.             TypeAndSpeak(
  192.                 "I can't open the pathname specifications window!\n");
  193.         }
  194.     }
  195.     return pathWindow;
  196. }
  197.  
  198. /* Set the current backup volume name into the curVolumeGadget. */
  199.  
  200. SetCurVolumeGadget(name)
  201.     char *name;
  202. {
  203.     long position;
  204.     char *s;
  205.  
  206.     position = RemoveGadget(mainWindow, &curVolumeGadget);
  207.     s = GadgetString((&curVolumeGadget));
  208.     strncpy(s, name, 30);
  209.     AddGadget(mainWindow, &curVolumeGadget, position);
  210.     RefreshGList(&curVolumeGadget, mainWindow, NULL, 1L);
  211. }
  212.  
  213. ^L
  214. /* Set the current errorCount value into the errorGadget. */
  215.  
  216. SetErrorGadget()
  217. {
  218.     ULONG position;
  219.     UBYTE *buffer;
  220.  
  221.     position = RemoveGadget(mainWindow, &errorGadget);
  222.     buffer = GadgetString((&errorGadget));
  223.     sprintf(buffer, "%4u", errorCount);
  224.     AddGadget(mainWindow, &errorGadget, position);
  225.     RefreshGList(&errorGadget, mainWindow, NULL, 1L);
  226. }
  227.  
  228. /* Adjust the disk "gauge" to show how full it currently is.
  229.  * Called with:
  230.  *         current:    number of blocks currently in use
  231.  *        maxValue:    total number of blocks available
  232.  */
  233.  
  234. SetGauge(current, maxValue)
  235.     LONG current, maxValue;
  236. {
  237.     ULONG hPot;
  238.     struct PropInfo *oldProp;
  239.     LONG range, used;
  240.  
  241. #ifdef DEBUG
  242.     sprintf(debugMsg,"SetGauge(%ld,%ld)\n",current,maxValue);
  243.     DebugWrite(debugMsg);
  244. #endif
  245.     oldProp = (struct PropInfo *) gaugeGadget.SpecialInfo;
  246.  
  247.     range = maxValue + 1;
  248.     used = maxValue - current;
  249.     hPot = (used << 16L) / range;
  250.     ModifyProp(
  251.         &gaugeGadget,                 /* gadget pointer */
  252.         mainWindow,                    /* window pointer */
  253.         NULL,                         /* requester */
  254.         (ULONG) oldProp->Flags,     /* flags */
  255.         hPot,                         /* horizPot */
  256.         0L,                            /* vertPot */
  257.         (ULONG) oldProp->HorizBody, /* horizBody */
  258.         (ULONG) oldProp->VertBody    /* vertBody */
  259.         );
  260. }
  261.  
  262. /* Handle IDCMP messages generated by user actions. */
  263.  
  264. User()
  265. {
  266.     ULONG class;                /* message class */
  267.     USHORT code;                /* message code */
  268.     USHORT gadgetID;                /* gadget ID */
  269.     APTR Iadr;                    /* address field from message */
  270.     struct IntuiMessage *msg;    /* Intuition message pointer */
  271.     struct Window *msgWindow;    /* window message occurred in */
  272.     USHORT quit = 0;
  273.     SHORT x,y;                    /* mouse x and y position */
  274.     ULONG waitBits;
  275.  
  276.  
  277. #ifdef DEBUG
  278.     sprintf(debugMsg,"User: waitBits = %08lx\n", waitBits);
  279.     DebugWrite(debugMsg);
  280. #endif
  281.  
  282.     while (!quit) {
  283.         ActivateWindow(mainWindow);
  284.  
  285.         waitBits = (1L << mainWindow->UserPort->mp_SigBit);
  286.         if (pathWindow)
  287.             waitBits = waitBits | (1L << pathWindow->UserPort->mp_SigBit);
  288.             Wait(waitBits);
  289.  
  290.         while (!quit) {
  291.             if (!(msg = (struct IntuiMessage *) 
  292.                 GetMsg(mainWindow->UserPort)))
  293.  
  294.             if ( ! (pathWindow && 
  295.                     (msg = (struct IntuiMessage *)
  296.                      GetMsg(pathWindow->UserPort) ) ) ) 
  297.                 break;
  298.  
  299.             class = msg->Class;
  300.             code = msg->Code;
  301.             Iadr = msg->IAddress;
  302.             x = msg->MouseX;
  303.             y = msg->MouseY;
  304.             msgWindow = msg->IDCMPWindow;
  305.             ReplyMsg(msg);        /* acknowledge the message */
  306. #ifdef DEBUG
  307.             sprintf(debugMsg,"Message class: 0x%lx, code: 0x%x\n",
  308.                 class, code);
  309. #endif
  310.             switch (class) {
  311.             case CLOSEWINDOW:
  312.                 if (msgWindow == mainWindow)
  313.                     ++quit;
  314.                 else if (msgWindow == pathWindow) {
  315.                     CloseWindow(pathWindow);
  316.                     pathWindow = NULL;
  317.                 }
  318.                 break;
  319.  
  320.             case GADGETUP:
  321.             case GADGETDOWN:
  322.                 DoGadget(msgWindow, class, Iadr);
  323.                 break;
  324.  
  325.             case MENUPICK:
  326.                 quit = UserMenu(code);
  327.                 break;
  328.  
  329.             default:
  330.                 break;            /* ignore the rest */
  331.             }                    /* end switch(class) */
  332.         }
  333.     }
  334. }
  335. /* Handle a menu selection. 
  336.  * Called with:
  337.  *        xcode:        menu selection code
  338.  * Returns:
  339.  *        status code (1 => Quit was selected)
  340.  */
  341.  
  342. int
  343. UserMenu(xcode)
  344.     USHORT xcode;
  345. {
  346.     USHORT code = xcode;
  347.     struct MenuItem *item;
  348.     USHORT itemNum,menuNum;
  349.  
  350.     while (code != MENUNULL) {
  351.         menuNum = MENUNUM(code);
  352.         itemNum = ITEMNUM(code);
  353.         item = ItemAddress(&MenuList1, (long) code);
  354.  
  355. #ifdef DEBUG
  356.         sprintf(debugMsg,
  357.                 "menu = %d, item = %d, flags = %04x\n",
  358.                 menuNum, itemNum, item->Flags);
  359.         DebugWrite(debugMsg);
  360. #endif
  361.  
  362.         switch (menuNum) {
  363.         case MENU_PROJECT:            /* Project Menu */
  364.             switch (itemNum) {
  365.             case ITEM_BACKUP:        /* Backup */
  366.                 Backup();
  367.                 break;
  368.             case ITEM_RESTORE:        /* Restore */
  369.                 Restore();
  370.                 break;
  371.             case ITEM_LOADPREFS:    /* Load Preferences */
  372.                 GetUserPrefs();
  373.                 break;
  374.             case ITEM_SAVEPREFS:    /* Save Preferences */
  375.                 PutUserPrefs();
  376.                 break;
  377.             case ITEM_ABOUT:        /* About */
  378.                 About();
  379.                 break;
  380.             case ITEM_QUIT:            /* Quit */
  381.                 return 1;            
  382.             default:
  383.                 DisplayBeep(NULL);
  384.                 break;
  385.             }
  386.             break;
  387.  
  388.         case MENU_FLAGS:            /* Flags Menu */
  389.             switch ( itemNum ) {
  390.             case ITEM_COMPRESS:        /* Compression */
  391.                 doCompress = IsChecked( item ); 
  392.                 break;
  393.             case ITEM_BIGFILES:        /* Do Big Files */
  394.                 doBigFiles = IsChecked( item );
  395.                 break;
  396.             case ITEM_LIST:            /* Listing */
  397.                 doListing = IsChecked( item );
  398.                 break;
  399.             case ITEM_SPEECH:        /* Speech */
  400.                 doSpeech = IsChecked( item );
  401.                 SetSpeech();
  402.                 break;
  403.             case ITEM_FORMAT:        /* Format output disks */
  404.                 doFormat = IsChecked( item );
  405.                 break;
  406.             default:
  407.                 DisplayBeep(NULL);
  408.                 break;
  409.             }
  410.             break;
  411.  
  412.         case MENU_WINDOWS:            /* Windows Menu */
  413.             OpenPathWindow();        /* There's only one item. */
  414.             break;
  415.         }
  416. #define EXTENDED_SELECT_WORKS
  417. #ifdef EXTENDED_SELECT_WORKS
  418.         code = item->NextSelect; 
  419.         /* This next line is a kludge.  Testing has revealed that
  420.          * the NextSelect field is returning 0000x.  Why?
  421.          */
  422.         if (!code) break;
  423. #else
  424.         break;
  425. #endif
  426.     }
  427.     return 0;
  428. }
  429.